/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws;

import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;
import ipa.Ipa;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author Peter
 */
@WebService(serviceName="IpaWS")
public class IpaWS {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "joinByColumns")
    public Image joinByColumns(@WebParam(name = "image1")
    Image image1, @WebParam(name = "image2")
    Image image2) {

        BufferedImage bi1 = (BufferedImage) image1;
        BufferedImage bi2 = (BufferedImage) image2;
        
        try {
            Ipa ipa = new Ipa();
            int w1 = bi1.getWidth();
            int w2 = bi2.getWidth();

            int h1 = bi1.getHeight();
            int h2 = bi2.getHeight();

            MWNumericArray data1 = getMWArray(w1, h1, bi1);
            MWNumericArray data2 = getMWArray(w2, h2, bi2);
            Object[] result = ipa.ipacolumn(1, w1, h1, data1, w2, h2, data2);

            BufferedImage bi = getBuffImage(result);

            return (Image)bi;
        } catch (Exception ex) {
            Logger.getLogger(IpaWS.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            System.gc();
            System.runFinalization();            
        }
        return null;
    }

     /**
     * Web service operation
     */
    @WebMethod(operationName = "joinByRows")
    public Image joinByRows(@WebParam(name = "image1")
    Image image1, @WebParam(name = "image2")
    Image image2) {

        BufferedImage bi1 = (BufferedImage) image1;
        BufferedImage bi2 = (BufferedImage) image2;

        try {
            Ipa ipa = new Ipa();
            int w1 = bi1.getWidth();
            int w2 = bi2.getWidth();

            int h1 = bi1.getHeight();
            int h2 = bi2.getHeight();

            MWNumericArray data1 = getMWArray(w1, h1, bi1);
            MWNumericArray data2 = getMWArray(w2, h2, bi2);
            Object[] result = ipa.iparow(1, w1, h1, data1, w2, h2, data2);

            BufferedImage bi = getBuffImage(result);

            return (Image)bi;
        } catch (Exception ex) {
            Logger.getLogger(IpaWS.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            System.gc();
            System.runFinalization();
        }
        return null;
    }

    private MWNumericArray getMWArray(int w, int h, BufferedImage bimage) {
        int[] iArray = new int[w * h * 3];
        int[] pix = bimage.getData().getPixels(0, 0, w, h, iArray);
        MWNumericArray mw = new MWNumericArray(pix);
        return mw;
    }

    private BufferedImage getBuffImage(Object[] result) throws IOException {
        MWNumericArray numericImageByteArray = (MWNumericArray) result[0];
        byte[] imageByteArray = numericImageByteArray.getByteData();
        ByteArrayInputStream bais = new ByteArrayInputStream(imageByteArray);
        return ImageIO.read(bais);
    }

}